<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>



<?php

//if ($_SERVER['REMOTE_ADDR'] != "88.100.10.78") {
if ($_SERVER['REMOTE_ADDR'] != "127.0.0.1") {
	echo "<p>You aren't allowed to access this site!. <br/> Get out!</p>";
	die;
} else {
	echo "<p>Acces allowed.</p>";
}

interface DBConnector {
	static function setInitialResource($resource);
	static function getInitialResource();
	/**
	 * General queries to the database.
	 *
	 * @param string $query
	 * @return resource
	 */
	function query($query);
}

// TODO It should be possible to have more different connectors - remove static variables.
class DBConnectorMySQL implements DBConnector {
	private static $username = 'eaiis';
	private static $password = 'welcome';
	private static $database = 'eaiis';
	private static $host = 'localhost';
	
	protected static $INIT_QUERIES = array ("SET NAMES 'utf8' COLLATE 'utf8_czech_ci'", 
		"SET CHARACTER SET utf8",
		"SET COLLATION_CONNECTION='utf8_czech_ci'"
	);
	
	private static $initialResource;
	static function setInitialResource($resource) {
		DBConnectorMySQL::$initialResource = $resource;
	}
	static function getInitialResource() {
		if (!is_resource(self::$initialResource)) {
			self::setInitialResource(mysql_connect(self::$host, self::$username, self::$password));
			foreach(self::$INIT_QUERIES as $initQuery) {
				echo "<p>Init query: " . mysql_query($initQuery) . "</p>";
			}
		}
		return self::$initialResource;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $query
	 * @return resource
	 */
	function query($query) {
		mysql_select_db(self::$database, self::getInitialResource());
		$res = mysql_query($query);
		if (!$res) {
			throw new Exception("Query `$query` failed.");
		} else { 
			return $res;
		}
	}
	/**
	 * Global indicator of started/nonstarted transaction.
	 */
	private static $isInTransaction = false;
	
	/**
	 * Recursively start a database transaction.
	 * In case that transaction is already on, nothing happens.
	 * 
	 * @return TransactionLocker - which is designed to use in DBConnectorMySQL::transactionCommit.
	 * @throws Exception - in case that database returns failure.
	 */
	public static function transactionBegin() {
		if (!self::$isInTransaction) {
			$connector = SiteContext::getSiteContext()->getDBConnector();
			$res = $connector->query("BEGIN;");
			self::$isInTransaction = true;
			return new TransactionLocker(true);
		} else {
			return new TransactionLocker(false);
		}
	}
	
	/**
	 * Roll back transaction in case that transaction was turned on by call 
	 * DBConnectorMySQL::transactionBegin.
	 * 
	 * @throws Exception - in case that database returns failure.
	 */
	public static function transactionRollback() {
		if (self::$isInTransaction) {
			$connector = SiteContext::getSiteContext()->getDBConnector();
			$res = $connector->query("ROLLBACK;");
		}
	}
	/**
	 * Commit a transaction in case that $tLocker was returned by a call which really started
	 * a transaction.
	 * 
	 * @param TransactionLocker $tLocker
	 */
	public static function transactionCommit(TransactionLocker $tLocker) {
		if ($tLocker->getIsTransactionStarter() && self::$isInTransaction) {
			$connector = SiteContext::getSiteContext()->getDBConnector();
			$res = $connector->query("COMMIT;");
		}
	}
}
/**
 * Class designed to be used with transaction operations
 * implemented in class DBConnectorMySQL.
 *
 */
class TransactionLocker {
	private $isTransactionStarter;
	
	public function __construct(bool $isStarter) {
		$this->isTransactionStarter = $isStarter;
	}
	public function getIsTransactionStarter() {
		return $this->isTransactionStarter;
	}
}

?>
<form method="post" action="dbconnector.php">
	<input style="width: 600px;" value="<?php if (isset($_POST['query'])) {echo $_POST['query'];} ?>" type="text" name="query" />
	<input type="submit" value="Submit" />
</form>
<?php 
	if (isset($_POST['query'])) {
	$query = $_POST['query'];

	echo "<p>QUERY: '$query'</p>";
	
	try {
		$dbConnector = new DBConnectorMySQL();

		$dbname = "eaiis";
		$dbuser = "eaiis";
		$dbpass = "welcome";
		$dbhost = "localhost";

		$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
		echo "backuping";
$command = "mysqldump --opt -h$dbhost -u$dbuser -p$dbpass $dbname | gzip > $backupFile";
echo system($command);
echo "END";
	die;	
	
	$result = $dbConnector->query("select * INTO OUTFILE 'test_backup.back' from tss_delivery");
	echo "<table>";
	
	for ($i = 0; $row = mysql_fetch_object($result); ++$i){
		echo "<tr>";
		if ($i == 0) {
			foreach ($row as $index => $value) {
				// write head
				echo "<td style=\"border: 1px solid black;\">";
				echo "<span style=\"font-weight: bold;\">$index</span>";
				echo "</td>";
			}
			echo "</tr><tr>";
		}
		foreach ($row as $index => $value) {
			echo "<td style=\"border: 1px solid black;\">";
			echo $value;
			echo "</td>";
		}
		echo "</tr>";
	}
	echo "</table>";
	
	} catch(Exception $e) {
		echo "<p>ERROR: $e</p>";
	}
}

?>

</div>
</body>
</html>
